package me.ywd.demo;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.dataflow.DataflowJobConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import me.ywd.job.MyDataflowJob;
import me.ywd.job.MyFirstElasticJob;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 *
 *
 *
 * @Author Ye_Wenda
 * @Date 6/27/2017
 */
@Component
public class SimpleStarterDemo {

    @PostConstruct
    public void start() {
        new JobScheduler(createRegistryCenter(), createJobConfiguration()).init();
        new JobScheduler(createRegistryCenter(), createDataflowJobConfiguration()).init();
    }

    private CoordinatorRegistryCenter createRegistryCenter() {
        CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration("127.0.0.1:2181", "ywd"));
        regCenter.init();
        return regCenter;
    }

    private LiteJobConfiguration createJobConfiguration() {
        // 定义作业核心配置
        JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder("demo1", "* */10 * * * ?", 5).build();
        // 定义SIMPLE类型配置
        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, MyFirstElasticJob.class.getCanonicalName());
        // 定义Lite作业根配置
        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).build();
        return simpleJobRootConfig;
    }

    private LiteJobConfiguration createDataflowJobConfiguration() {
        // 定义作业核心配置
        // "*/10 * * * * ?" 每10s从下一分钟开始
        // "0/10 * * * * ?" 每10s从现在开始
        JobCoreConfiguration coreConfiguration = JobCoreConfiguration.newBuilder("demo2", "0/45 * * * * ?", 5).build();
        // 定义SIMPLE类型配置
        DataflowJobConfiguration dataflowJobConfiguration = new DataflowJobConfiguration(coreConfiguration, MyDataflowJob.class.getCanonicalName(), false);
        // 定义Lite作业根配置
        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(dataflowJobConfiguration).build();
        return simpleJobRootConfig;
    }
}
